/*
 * 文件名：CellInfoService.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2016年3月29日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.service.base.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.CellMenu;
import com.zcsy.manage.persistence.base.CellInfoDao;
import com.zcsy.manage.service.base.ICellBuildingService;
import com.zcsy.manage.service.base.ICellHouseService;
import com.zcsy.manage.service.base.ICellInfoService;
import com.zcsy.manage.service.base.ICellRegionService;
import com.zcsy.manage.service.base.ICellUnitService;
import com.zcsy.manage.service.system.impl.SystemComService;

@Service
public class CellInfoService extends SystemComService implements ICellInfoService {

	private static final String ZERO = "0";//造的假数据
	
	@Autowired
	private CellInfoDao cellInfoDao;
	
	@Autowired
    private ICellRegionService cellRegionService;
    
    @Autowired
    private ICellBuildingService cellBuildingService;
    
    @Autowired
    private ICellUnitService cellUnitService;
    
    @Autowired
    private ICellHouseService cellHouseService;
	
	@Override
	public int insert(CellInfo cellInfo) {
		cellInfo.setSendTime(new Date());
		return cellInfoDao.insert(cellInfo);
	}

	@Override
	public int update(CellInfo cellInfo) {
	    CellInfo oldInfo = cellInfoDao.selectByPrimaryKey(cellInfo.getId());
	    
	    //如果小区名称修改了，则同步修改所有区域所在的小区名称
        if(!(oldInfo.getCellName()).equals(cellInfo.getCellName())){
            cellRegionService.updateById(oldInfo.getId(), cellInfo.getCellName());
            cellBuildingService.updateById(oldInfo.getId(), cellInfo.getCellName(),Constants.CELL_TYPE);
            cellUnitService.updateById(oldInfo.getId(), cellInfo.getCellName(), Constants.CELL_TYPE);
            cellHouseService.updateById(oldInfo.getId(), cellInfo.getCellName(), Constants.CELL_TYPE);
        }
	    
	    oldInfo.setCellName(cellInfo.getCellName());;
	    oldInfo.setProvinceId(cellInfo.getProvinceId());
	    oldInfo.setCityId(cellInfo.getCityId());
	    oldInfo.setAreaId(cellInfo.getAreaId());
	    oldInfo.setCellAddress(cellInfo.getCellAddress());
	    oldInfo.setAreaLand(cellInfo.getAreaLand());
	    oldInfo.setAreaConstruction(cellInfo.getAreaConstruction());
	    oldInfo.setCellDescription(cellInfo.getCellDescription());
	    oldInfo.setSort(cellInfo.getSort());
	    oldInfo.setLongitude(cellInfo.getLongitude());
	    oldInfo.setLatitude(cellInfo.getLatitude());
	    oldInfo.setOperationCompanyId(cellInfo.getOperationCompanyId());
	    oldInfo.setProCompanyId(cellInfo.getProCompanyId());
	    oldInfo.setDisCompanyId(cellInfo.getDisCompanyId());
	    oldInfo.setLogo(cellInfo.getLogo());
	    oldInfo.setUpdateTime(cellInfo.getUpdateTime());
	    
	    oldInfo.setProvinceName(cellInfo.getProvinceName());
	    oldInfo.setCityName(cellInfo.getCityName());
	    oldInfo.setAreaName(cellInfo.getAreaName());
	    
        
		return cellInfoDao.update(oldInfo);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		return cellInfoDao.deleteByPrimaryKey(id);
	}

	@Override
	public CellInfo selectByPrimaryKey(String id) {
		return cellInfoDao.selectByPrimaryKey(id);
	}

	@Override
	public List<CellInfo> selectByKeyword(CellInfo cellInfo) {
		return cellInfoDao.selectByKeyword(cellInfo);
	}

	@Override
	public List<CellInfo> getAllCellInfo() {
		return cellInfoDao.getAllCellInfo();
	}

	@Override
	public List<CellInfo> getCityListByProvinceId(Integer provinceId,
			@Param(value = "cellIds") List<String> cellIds) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("provinceId", provinceId);
		map.put("cellIds", cellIds);
		return cellInfoDao.getCityListByProvinceId(map);
	}

	@Override
	public List<CellInfo> getCellListByCityId(Integer cityId,
			@Param(value = "cellIds") List<String> cellIds) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cityId", cityId);
		map.put("cellIds", cellIds);
		return cellInfoDao.getCellListByCityId(map);
	}

	@Override
	public List<String> getMenusByCellId(String cellId , int type){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cellId", cellId);
		map.put("system", type);
		return cellInfoDao.getMenusByCellId(map);
	}
	
	@Override
	public void updateCellMenu(String cellId , String []menus){
		/**
         * 如果不管menus是否为空，均先清空权限
         */
		cellInfoDao.deleteCellMenus(cellId);
        if(menus != null && menus.length > 0) {
            for(String menuId : menus){
            	if(!menuId.equals(ZERO)){
            		CellMenu cellMenu = new CellMenu();
            		cellMenu.setMenuId(menuId);
            		cellMenu.setCellId(cellId);
            		cellMenu.setId(UniqueNumberUtils.get18UniqueNumber());
                    cellInfoDao.insertCellMenu(cellMenu);
            	}
            }
        }
	}

	@Override
	public List<CellInfo> getCellsAndRegions(String cityId) {
		return cellInfoDao.getCellsAndRegions(cityId);
	}

	@Override
	public List<CellInfo> getCells(String cityId) {
		return cellInfoDao.getCells(cityId);
	}

    @Override
    public boolean isNameExists(String cellId, String name) {
        return cellInfoDao.isNameExists(cellId, name);
    }

	@Override
	public int updateSort(CellInfo record) {
		CellInfo oldInfo = cellInfoDao.selectByPrimaryKey(record.getId());
		
		oldInfo.setSort(record.getSort());
		oldInfo.setUpdateTime(record.getUpdateTime());
		
		return cellInfoDao.updateSort(oldInfo);
	}

	@Override
	public Integer selectCountByOIds(String[] companyIds) {
		return cellInfoDao.selectCountByOIds(companyIds);
	}

	@Override
	public Integer selectCountByDIds(String[] companyIds) {
		return cellInfoDao.selectCountByDIds(companyIds);
	}

	@Override
	public Integer selectCountByPIds(String[] companyIds) {
		return cellInfoDao.selectCountByPIds(companyIds);
	}
    
}
